package com.whiteape.book.core.engine;

import com.whiteape.book.model.bo.RuleBo;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

public class JsoupSelectorEngine {

    /**
     * @param parent 父类元素
     * @param rule   选择器规则  rule && op
     * @return
     */
    public static String checkAndParse(Element parent, String rule) {

        if (parent == null || StringUtils.isEmpty(rule)) {
            return "";
        }

        //如果是非列表类（如多个li)的，正常一个元素的选择器，在后面需要有元素的后续处理动作
        if (rule.indexOf("&&") <= 0) {
            return "";
        }

        String[] towParts = rule.split("&&");
        String selector = towParts[0].trim();
        String op = towParts[1].trim();

        Element nodeElement;
        if (StringUtils.isEmpty(selector)) {
//            return "";
            nodeElement = parent;
        } else {
            Elements nodeElements = parent.select(selector);
            if (nodeElements == null || nodeElements.size() == 0) {
                return "";
            }
            nodeElement = nodeElements.get(0);
        }
        //TODO 是取属性还是取text
        String result;
        if ("text".equals(op)) {
            result = nodeElement.text();
        } else {
            result = nodeElement.attr(op);
        }
        return result.trim();
    }


    public static String getString(String doc, String rule) {

        if (doc == null || StringUtils.isEmpty(rule)) {
            return "";
        }

        doc = preProcess(doc);
        //如果是非列表类（如多个li)的，正常一个元素的选择器，在后面需要有元素的后续处理动作
        if (!rule.contains("@")) {
            return "";
        }

        String[] towParts = rule.split("@");
        String selector = towParts[0].trim();
        String op = towParts[1].trim();

        Document parent = Jsoup.parse(doc);
        Element nodeElement;
        if (StringUtils.isEmpty(selector)) {
            if (parent.body() != null && parent.body().child(0) != null) {
                nodeElement = parent.body().child(0);
            }else {
                nodeElement = new Element(doc);
            }
        }else {
            Elements nodeElements = parent.select(selector);
            if (nodeElements == null || nodeElements.size() == 0) {
                return "";
            }
            nodeElement = nodeElements.get(0);
        }
        String result;
        if ("text".equals(op)) {
            result = nodeElement.text();
        } else if ("html".equals(op)) {
            result = nodeElement.html();
        } else {
            result = nodeElement.attr(op);
        }
        return result.trim();

    }

    /**
     * 如果是多个的，类似  e1,e2,e3表示任意满足其中一个的，都会选择,可能会重复，需要合并？
     *
     * @param html
     * @param rule
     * @return
     */
    public static List<Element> getElementList(String html, String rule) {
        List<Element> result = new ArrayList<>();
        if (StringUtils.isEmpty(html) || StringUtils.isEmpty(rule)) {
            return result;
        }
        html = preProcess(html);
        Document doc = Jsoup.parse(html);
        return doc.select(rule);


    }

    private static String preProcess(String html) {
        if (html.endsWith("</td>")) {
            html = String.format("<tr>%s</tr>", html);
        }
        if (html.endsWith("</tr>") || html.endsWith("</tbody>")) {
            html = String.format("<table>%s</table>", html);
        }
        return html;
    }
}
